/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vasthu.api.domain;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author moremada This class holds information about the whole items which
 * were purchased from the stock and the information about billing.
 */
public class BillInfo {

    private List<PurchasedItem> purchasedItems;
    private Bill bill;

    public BillInfo(Bill bill) {
        this.bill = bill;
        //purchasedItems = new ArrayList<PurchasedItem>();
    }

    /**
     * @return the purchasedItems
     */
    public List<PurchasedItem> getPurchasedItems() {
        return purchasedItems;
    }

    public PurchasedItem getPurchasedItemByRowNumber(int rowNo){
        PurchasedItem pItem = null;
        if(rowNo > 0){
            for(PurchasedItem p : purchasedItems){
                if(rowNo == p.getRowNumber()){
                    pItem = p;
                    break;
                }
            }
        }
        return pItem;
    }
    
    
    /**
     * @param purchasedItems the purchasedItems to set
     */
    public void setPurchasedItems(List<PurchasedItem> purchasedItems) {
        this.purchasedItems = purchasedItems;
    }

    public void addToCart(PurchasedItem purchasedItem) {
        if (purchasedItems == null) {
            purchasedItems = new ArrayList<PurchasedItem>();
        }
        if (purchasedItem != null) {
            purchasedItems.add(purchasedItem);
            calculateTotal(purchasedItem);
        }
    }

    public void update(PurchasedItem purchasedItem){
        
    }
    
    public void removeFromCart(PurchasedItem purchasedItem) {
        if (purchasedItem != null) {
            //decreaseBill(purchasedItem);
            //purchasedItems.remove(purchasedItem);
        }
    }

    /**
     * @return the bill
     */
    public Bill getBill() {
        return bill;
    }

    /**
     * @param bill the bill to set
     */
    public void setBill(Bill bill) {
        this.bill = bill;
    }

    private void calculateTotal(PurchasedItem purchasedItem) {
        bill.setTotalAmount(bill.getTotalAmount() + purchasedItem.calculateSubTotal());
    }

    public void suspend() {
        bill.setStatus(2);
    }

    private void decreaseBill(PurchasedItem purchasedItem) {
        bill.setTotalAmount(bill.getTotalAmount() - purchasedItem.getSubTotal());
    }
    
    public void updateStockQuantity(){
        for(PurchasedItem p : purchasedItems){
            p.updateStockQuantity();
        }
    }
}
